/**
 * Copyright (C) Skywares Information Technology, LTD. 
 * All Rights Reserved.
 *
 * MiaoshaService.java created on 2020年2月4日 下午7:35:35 by Lyon Lu 
 */
package com.mvc.server.miaosha.service;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mvc.server.component.RedisService;
import com.mvc.server.miaosha.config.DataInit;
import com.mvc.server.miaosha.pojo.MiaoshaOrder;
import com.mvc.server.miaosha.pojo.MiaoshaUser;
import com.mvc.server.miaosha.pojo.OrderInfo;
import com.mvc.server.miaosha.redis.GoodsKey;
import com.mvc.server.miaosha.redis.MiaoshaKey;
import com.mvc.server.miaosha.util.MD5Util;
import com.mvc.server.miaosha.util.UUIDUtil;
import com.mvc.server.miaosha.vo.GoodsVo;

/**
 * <pre>
 * Description:
 * 
 * @author Lyon Lu
 * @date 2020年2月4日 下午7:35:35
 *
 * </pre>
 */
@Service
public class MiaoshaService {

	@Resource
	private GoodsService goodsService;
	
	@Resource
	private OrderService orderService;
	
	@Resource
	private RedisService redisService;
	
	/**
	 * Description:
	 * 		method description here
	 * @param user
	 * @param goods
	 * @return OrderInfo      
	 * @throws                                 
	 */
	@Transactional
	public OrderInfo miaosha(MiaoshaUser user, GoodsVo goods) {
		boolean success = goodsService.reduceStock(goods);
		if(success) {
			return orderService.createOrder(user, goods);
		}
		else {
			setGoodsStockOver(goods.getId());
			return null;
		}
	}
	
	public Long getMiaoshaResult(Long userId, long goodsId) {
		MiaoshaOrder order = orderService.getCacheMiaoshaOrderByUserIdGoodsId(userId, goodsId);
		if(order != null) {//秒杀成功
			return order.getOrderId();
		}else {
			boolean isOver = getGoodsStockOver(goodsId);
			if(!isOver) {//此商品的秒杀还没结束，返回处理中
				return 0l;
			}else {//此商品的秒杀已经结束，但是可能订单还在生成中
				//获取所有的秒杀订单, 判断订单数量和参与秒杀的商品数量
				List<MiaoshaOrder> orders = orderService.getAllMiaoshaOrdersByGoodsId(goodsId);
				if(orders == null || orders.size() < DataInit.getGoodsStockOriginal(goodsId)){
					return 0l;//订单还在生成中
				}else {//判断是否有此用户的订单
					MiaoshaOrder o = getMiaoshaOrderByAllOrders(orders, userId);
					if(o != null) {//如果有，则说明秒杀成功
						return o.getOrderId();
					}else {//秒杀失败
						return -1l;
					}
				}
			}
		}
	}
	
	private MiaoshaOrder getMiaoshaOrderByAllOrders(List<MiaoshaOrder> orders, Long userId) {
		if(orders == null || orders.size() <= 0) {
			return null;
		}
		for(MiaoshaOrder order : orders) {
			if(order.getUserId().equals(userId)) {
				return order;
			}
		}
		return null;
	}
	
	private void setGoodsStockOver(Long goodsId) {
		redisService.set(GoodsKey.isGoodsStockOver, String.valueOf(goodsId), true);
	}
	
	private Boolean getGoodsStockOver(long goodsId) {
		return redisService.hasKey(GoodsKey.isGoodsStockOver, String.valueOf(goodsId));
	}
	
	public boolean checkPath(MiaoshaUser user, long goodsId, String path) {
		if(user == null || path == null) {
			return false;
		}
		String pathOld = redisService.get(MiaoshaKey.getMiaoshaPath, ""+user.getId() + "_"+ goodsId, String.class);
		return path.equals(pathOld);
	}

	public String createMiaoshaPath(MiaoshaUser user, long goodsId) {
		if(user == null || goodsId <=0) {
			return null;
		}
		String str = MD5Util.md5(UUIDUtil.uuid()+"123456");
    	redisService.set(MiaoshaKey.getMiaoshaPath, ""+user.getId() + "_"+ goodsId, str);
		return str;
	}

	public BufferedImage createVerifyCode(MiaoshaUser user, long goodsId) {
		if(user == null || goodsId <=0) {
			return null;
		}
		int width = 80;
		int height = 32;
		//create the image
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		// set the background color
		g.setColor(new Color(0xDCDCDC));
		g.fillRect(0, 0, width, height);
		// draw the border
		g.setColor(Color.black);
		g.drawRect(0, 0, width - 1, height - 1);
		// create a random instance to generate the codes
		Random rdm = new Random();
		// make some confusion
		for (int i = 0; i < 50; i++) {
			int x = rdm.nextInt(width);
			int y = rdm.nextInt(height);
			g.drawOval(x, y, 0, 0);
		}
		// generate a random code
		String verifyCode = generateVerifyCode(rdm);
		g.setColor(new Color(0, 100, 0));
		g.setFont(new Font("Candara", Font.BOLD, 24));
		g.drawString(verifyCode, 8, 24);
		g.dispose();
		//把验证码存到redis中
		int rnd = calc(verifyCode);
		redisService.set(MiaoshaKey.getMiaoshaVerifyCode, user.getId()+","+goodsId, rnd);
		//输出图片	
		return image;
	}

	public boolean checkVerifyCode(MiaoshaUser user, long goodsId, int verifyCode) {
		if(user == null || goodsId <=0) {
			return false;
		}
		Integer codeOld = redisService.get(MiaoshaKey.getMiaoshaVerifyCode, user.getId()+","+goodsId, Integer.class);
		if(codeOld == null || codeOld - verifyCode != 0 ) {
			return false;
		}
		redisService.delete(MiaoshaKey.getMiaoshaVerifyCode, user.getId()+","+goodsId);
		return true;
	}
	
	private static int calc(String exp) {
		try {
			ScriptEngineManager manager = new ScriptEngineManager();
			ScriptEngine engine = manager.getEngineByName("JavaScript");
			return (Integer)engine.eval(exp);
		}catch(Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	private static char[] ops = new char[] {'+', '-', '*'};
	/**
	 * + - * 
	 * */
	private String generateVerifyCode(Random rdm) {
		int num1 = rdm.nextInt(10);
	    int num2 = rdm.nextInt(10);
		int num3 = rdm.nextInt(10);
		char op1 = ops[rdm.nextInt(3)];
		char op2 = ops[rdm.nextInt(3)];
		String exp = ""+ num1 + op1 + num2 + op2 + num3;
		return exp;
	}

}
